<script type="text/javascript">
    $(function() {
        $("#pages_list ul").sortable({
            revert: true,
            update: function (event,ui){
                idcurrent = ui.item.children().children().attr('href');
                idnext = ui.item.prev().children().children().attr('href');

                id_top = idcurrent.substr(idcurrent.lastIndexOf('/') + 1);
                if (idnext == null) {
                    id_down = 0;
                } else {
                    id_down = idnext.substr(idnext.lastIndexOf('/') + 1);
                }
                url = '<?php echo ADMIN_URL; ?>/pages/fly/id/' + id_top + '/cid/' + id_down;
                $("#preloader").show();
                $.ajax({
                    url: url,
                    cache: false,
                    dataType: "html",
                    success: function(message)
                    {
                        $("#preloader").hide();
                        $("#ajax_content").empty().append(message);
                    }
                });


            }
        });

        $("#pages_list").treeview({
            persist: "location",
            collapsed: true,
            unique: false
        });
                
        $("li > div > a").nextAll().hide();
        $("li > div").hover(
        function () {
            $('> a',this).nextAll().show();
        },
        function () {
            $('> a',this).nextAll().hide();
        }
    );
                    

        $(".acl_button").click(function () {
            url = $(this).attr("id");
            $.ajax({
                url: url,
                cache: false,
                dataType: "html",
                success: function(message)
                {
                    $("#dialog").empty().append(message);
                }
            });
        });
    });
</script>

<div id="page_content">
    <!-- start pages -->
    <div id="pages_list">
        <?php
        $pages = $this->pages;
        $level = 0;
        foreach ($pages as $key => $val):
            if ($level > $val->position) {
                echo str_repeat('</li></ul>', $level - $val->position);
            }
            if ($level >= $val->position) {
                echo("</li>");
            }

            if ($level < $val->position) {
                echo '<ul>';
            }
            $level = $val->position;
            $list_style = 'visible_' . $val->visible;
            if ($val->visible == -1)
                $link = 1;
            else
                $link = 0;
            ?>
            <li>
                <div class="<?php echo $list_style ?>">
                    <?php if ($val->acl > 1 && $val->visible <> -1) { ?>
                        <a href="<?php echo ADMIN_URL; ?>/pages/section/id/<?php echo $val->id ?>"><?php echo $val->name ?></a>
                    <?php } else { 
                        if ($val->visible <> -1) { ?>
                        <a href="#" class="only_read"><?php echo $val->name ?></a>
                    <?php
                        } else {
                            ?>
                            <a href="<?php echo ADMIN_URL; ?>/pages/edit/link/1/id/<?php echo $val->id ?>"><?php echo $val->name ?></a>
                            <?php
                        }
                    }
                    if ($val->acl > 0) {
                        if ($link == 0) {
                            ?>
                            <a href="<?php echo SITE_URL . $val->url ?>" title="<?php echo $this->t->_("View") ?>" target="_blank" class="nav_button view_button"></a>
                    <?php } else { ?>
                             <a href="<?php echo $val->url ?>" title="<?php echo $this->t->_("View") ?>" target="_blank" class="nav_button view_button"></a>
                            <?php
                        }
                    }
                    if ($val->acl > 1) {
                        ?>
                        <a href="<?php echo ADMIN_URL; ?>/pages/edit/id/<?php echo $val->id ?>/link/<?= $link ?>" title="<?php echo $this->t->_("Settings") ?>" class="nav_button setting_button"></a>
                    <?php }
                    if ($val->acl > 2) { ?>
                        <a href="#" id="<?php echo ADMIN_URL ?>/acl/acl/object/pages/oid/<?php echo $val->id ?>/aid/<?php echo $val->aid ?>" title="<?php echo $this->t->_("Access") ?>" class="nav_button acl_button"></a>
                        <a href="#" id="<?php echo ADMIN_URL ?>/pages/delete/id/<?php echo $val->id ?>" title="<?php echo $this->t->_("Delete") ?>" id="delete_action" class="nav_button delete_button"></a>
    <?php } ?>
                    &nbsp;
                </div>
                <?php
            endforeach;
            echo '</li>' . str_repeat('</ul>', $level);
            ?>
    </div>
    <!-- end pages -->
</div>

<div id="sidebar">
    <a href="<?php echo ADMIN_URL . '/pages/edit/' ?>" class="sidebar_button add_button"><?php echo $this->t->_("New page") ?></a><br />
    <a href="<?php echo ADMIN_URL . '/pages/edit/link/1' ?>" class="sidebar_button add_link_button"><?php echo $this->t->_("New link") ?></a>
</div>

